using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

// static 静态类； 管理类不需要MonoBehaviour
public static class PanelManager
{
    public static bool isInit;

    /// <summary>
    /// 层级
    /// </summary>
    public enum Layer
    {
        Panel,
        Tip,
    }

    // 静态类，后面都要写 static？
    /// <summary>
    /// UI层级列表: layers,有Panel类型和Tip类型,key:value 对应 游戏物体 canvas.Find("Panel") 和canvas.Find("Tip")
    /// </summary>
    private static Dictionary<Layer, Transform> layers = new Dictionary<Layer, Transform>();

    /// <summary>
    /// panels 面板列表； 值是字符串：去对应 具体的panel身上的脚本
    /// </summary>
    private static Dictionary<string, BasePanel> panels = new Dictionary<string, BasePanel>();

    /// <summary>
    /// 根节点
    /// </summary>
    private static Transform root;

    /// <summary>
    /// 画布
    /// </summary>
    private static Transform canvas;

    /// <summary>
    /// 绑定对应关系，完成初始化 layers 字典
    /// </summary>
    public static void Init()
    {
        Debug.Log("panelManager Init");
        if (isInit)
            return;
        root = GameObject.Find("Root").transform;
        canvas = root.Find("Canvas");
        Transform Panel = canvas.Find("Panel");
        Transform Tip = canvas.Find("Tip");
        // if (!panels.ContainsKey("Panel"))
        // {
        //     layers.Add(Layer.Panel, Panel);
        // }
        // if (!panels.ContainsKey("Tip"))
        // {
        //     layers.Add(Layer.Tip, Tip);
        // }
        layers.Add(Layer.Panel, Panel);
        layers.Add(Layer.Tip, Tip);
    }

    /// <summary>
    /// 打开面板
    /// </summary>
    /// <typeparam name="T">面板脚本</typeparam>
    public static void Open<T>()
        where T : BasePanel
    {
        // 检测是否已经打开
        string name = typeof(T).ToString(); // 获取面板名字
        Debug.Log("name");
        Debug.Log(name);
        if (panels.ContainsKey(name))
        {
            return;
        }
        // 如果没有打开，给根节点添加 BasePanel 的子类 T脚本
        BasePanel panel = root.AddComponent<T>();
        // OnInit 可以获取到加载路径
        panel.OnInit();
        panel.Init();
        // Debug.Log("panel.layer");
        // Debug.Log(panel.layer);
        Transform layer = layers[panel.layer];

        // 将 子panel panel.skin.transform 设置在 Panel.layer 上, 而Panel.Tip 和Panel.layer都在Root.Canvas上,所以才能显示出来
        // 得到的UI树是 Canvas => Panel.layer => panel.skin.transform(mainPanel)
        panel.skin.transform.SetParent(layer, false);

        panels.Add(name, panel);
        panel.OnShow();
    }

    /// <summary>
    /// 关闭面板
    /// </summary>
    /// <param name="name"></param>
    public static void Close(string name)
    {
        isInit = false;
        // 如果不存在字典中，直接放
        if (!panels.ContainsKey(name))
        {
            return;
        }
        // Debug.Log("Close");
        // 如果存在，获取到相应的脚本
        BasePanel panel = panels[name];
        panel.OnClose(); // 执行OnClose
        panels.Remove(name); // 移除
        layers.Remove(Layer.Panel);
        layers.Remove(Layer.Tip);
        GameObject.Destroy(panel.skin); // 游戏对象的面板
        GameObject.Destroy(panel); // 脚本也要销毁
    }
}
